package com.schedule.server.serverImpl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.schedule.daoVo.ScheduleVo;
import com.schedule.dto.ScheduleDto;
import com.schedule.dto.SchedulePageDto;
import com.schedule.entity.Schedule;
import com.schedule.mapper.ScheduleMapper;
import com.schedule.result.PageResult;
import com.schedule.server.ScheduleService;
import com.schedule.utils.UserIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServerImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;

    /***
     *
     * 获取该用户的所有日志计划
     * ***/
    public List<ScheduleVo> SelectData() {
           Long id = UserIdUtil.getUserId();
          return scheduleMapper.selectByUserId(id);
    }
    /**
     * @param scheduleDto
     * @return 添加日志数据
     * ***/
    public void CISchedule(ScheduleDto scheduleDto) {
        Schedule newSchedule = new Schedule();
        newSchedule.setUserId(UserIdUtil.getUserId());
        BeanUtils.copyProperties(scheduleDto,newSchedule);
        scheduleMapper.InsertSchedule(newSchedule);
    }
    /***
     * 分页查询
     * @param schedulePageDto
     * @return**/
    public PageResult PageQuery(SchedulePageDto schedulePageDto) {

        PageHelper.startPage(schedulePageDto.getPage(),schedulePageDto.getPageSize());
        Page<Schedule> page = scheduleMapper.pageQuery(schedulePageDto);
//  获取分页结果
        long total = page.getTotal();        // 总记录数
        List<Schedule> records = page.getResult(); // 当前页的数据列表

//  返回封装的分页结果
        return new PageResult(total, records);
    }

    /*****
     * @param ids
     * @return 批量删除数据
     * ***/
    public int DeleteSchedulesByIds(List<Long> ids) {
        return scheduleMapper.deleteSchedulesByIds(ids);

    }
}
